Imports DotNetNuke.Framework.Providers
Imports DotNetNuke.Common.Utilities
Imports DotNetNuke.Common.Utilities.Config
Imports System.Data
Imports System.Data.SqlClient

Public Class ProductController
    ' Methods
    Public Sub New()
        Dim provider As Provider = CType(Me._providerConfiguration.Providers.Item(Me._providerConfiguration.DefaultProvider), Provider)
        Me._objectQualifier = provider.Attributes.Item("objectQualifier")
        If (Me._objectQualifier <> String.Empty And Not Me._objectQualifier.EndsWith("_")) Then
            Me._objectQualifier = (Me._objectQualifier & "_")
        End If
        Me._databaseOwner = provider.Attributes.Item("databaseOwner")
        If (Me._databaseOwner <> String.Empty And Not Me._databaseOwner.EndsWith(".")) Then
            Me._databaseOwner = (Me._databaseOwner & ".")
        End If
    End Sub

    Public Sub DeleteProduct(ByVal ProductId As Integer)
        Try
            Dim connection As New SqlConnection(Me.ConnectionString)
            Dim command As New SqlCommand(DatabaseOwner & ObjectQualifier & "activeapc_dnn_Products_Delete", connection)
            command.CommandType = CommandType.StoredProcedure
            command.Parameters.Add("@ProductId", SqlDbType.Int).Value = (ProductId)
            connection.Open()
            command.ExecuteNonQuery()
            connection.Close()
        Catch ex As Exception

        End Try
    End Sub

    Public Sub DeleteProductNote(ByVal ProductId As Integer, ByVal FeatureId As Integer)
        Try
            Dim connection As New SqlConnection(Me.ConnectionString)
            Dim command As New SqlCommand(DatabaseOwner & ObjectQualifier & "activeapc_dnn_Products_DeleteNote", connection)
            command.CommandType = CommandType.StoredProcedure
            command.Parameters.Add("@ProductId", SqlDbType.Int).Value = (ProductId)
            command.Parameters.Add("@FeatureId", SqlDbType.Int).Value = (FeatureId)
            connection.Open()
            command.ExecuteNonQuery()
            connection.Close()
        Catch ex As Exception

        End Try
    End Sub

    Private Function GetNull(ByVal Field As Object) As Object
        Return Null.GetNull((Field), DBNull.Value)
    End Function

    Public Function GetProduct(ByVal ProductId As Integer) As ProductInfo
        Try
            Dim pi As New ProductInfo
            Dim connection As New SqlConnection(Me.ConnectionString)
            Dim command As New SqlCommand(DatabaseOwner & ObjectQualifier & "activeapc_dnn_Products_GetByProductId", connection)
            command.CommandType = CommandType.StoredProcedure
            command.Parameters.Add("@ProductId", SqlDbType.Int).Value = ProductId
            connection.Open()
            Dim reader As IDataReader = command.ExecuteReader
            Do While reader.Read
                pi.ProductId = ProductId
                pi.ProductGroupId = CInt(reader.Item("ProductGroupId"))
                pi.Price = CDec(reader.Item("Price"))
                pi.Product = Convert.ToString(reader.Item("Product"))
                pi.SortOrder = CInt(reader.Item("SortOrder"))
                pi.DisplayPrice = CBool(reader.Item("DisplayPrice"))
            Loop
            reader.Close()
            reader.Dispose()
            command.Dispose()
            connection.Close()
            connection.Dispose()
            Return pi
        Catch ex As Exception
            DotNetNuke.Services.Exceptions.LogException(ex)
            Return Nothing
        End Try
    End Function

    Public Function GetProductFeatures(ByVal FeatureGroupId As Integer) As IDataReader
        Try
            Dim connection As New SqlConnection(Me.ConnectionString)
            Dim command As New SqlCommand(DatabaseOwner & ObjectQualifier & "activeapc_dnn_ProductFeatures_Get", connection)
            command.CommandType = CommandType.StoredProcedure
            command.Parameters.Add("@FeatureGroupId", SqlDbType.Int).Value = FeatureGroupId
            connection.Open()
            Return command.ExecuteReader(CommandBehavior.CloseConnection)
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Public Function GetProductListByProductGroupId(ByVal ProductGroupId As Integer) As IDataReader
        Try
            Dim connection As New SqlConnection(Me.ConnectionString)
            Dim command As New SqlCommand(DatabaseOwner & ObjectQualifier & "activeapc_dnn_Products_GetByProductGroupId", connection)
            command.CommandType = CommandType.StoredProcedure
            command.Parameters.Add("@ProductGroupId", SqlDbType.Int).Value = ProductGroupId
            connection.Open()
            Return command.ExecuteReader(CommandBehavior.CloseConnection)
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function GetProductAndFeaturesWithNotes(ByVal FeatureGroupId As Integer) As IDataReader
        Try
            Dim connection As New SqlConnection(Me.ConnectionString)
            Dim command As New SqlCommand(DatabaseOwner & ObjectQualifier & "activeapc_dnn_ProductFeatures_GetNotes", connection)
            command.CommandType = CommandType.StoredProcedure
            command.Parameters.Add("@FeatureGroupId", SqlDbType.Int).Value = FeatureGroupId
            connection.Open()
            Return command.ExecuteReader(CommandBehavior.CloseConnection)
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function SaveProduct(ByVal Prod As ProductInfo) As Integer
        Try
            Dim connection As New SqlConnection(Me.ConnectionString)
            Dim command As New SqlCommand(DatabaseOwner & ObjectQualifier & "activeapc_dnn_Products_Save", connection)
            command.CommandType = CommandType.StoredProcedure
            command.Parameters.Add("@ProductId", SqlDbType.Int).Value = Prod.ProductId
            command.Parameters.Add("@Product", SqlDbType.NVarChar, &HFF).Value = Prod.Product
            command.Parameters.Add("@ProductGroupId", SqlDbType.Int).Value = Prod.ProductGroupId
            command.Parameters.Add("@Price", SqlDbType.Money).Value = Prod.Price
            command.Parameters.Add("@SortOrder", SqlDbType.Int).Value = Prod.SortOrder
            command.Parameters.Add("@DisplayPrice", SqlDbType.Bit).Value = Prod.DisplayPrice
            connection.Open()
            Prod.ProductId = CInt(command.ExecuteScalar)
            connection.Close()
            command.Dispose()
            connection.Dispose()
            Return Prod.ProductId
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function SaveProductNote(ByVal ProductId As Integer, ByVal FeatureId As Integer, ByVal SupportValue As Integer, ByVal Text As String) As Boolean
        Try
            Dim connection As New SqlConnection(Me.ConnectionString)
            Dim command As New SqlCommand(DatabaseOwner & ObjectQualifier & "activeapc_dnn_ProductFeatures_SaveNote", connection)
            command.CommandType = CommandType.StoredProcedure
            command.Parameters.Add("@ProductId", SqlDbType.Int).Value = ProductId
            command.Parameters.Add("@FeatureId", SqlDbType.Int).Value = FeatureId
            command.Parameters.Add("@SupportValue", SqlDbType.Int).Value = SupportValue
            command.Parameters.Add("@Text", SqlDbType.NVarChar, 255).Value = Text
            connection.Open()
            command.ExecuteNonQuery()
            connection.Close()
            command.Dispose()
            connection.Dispose()
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
    Public Function SaveProductFeature(ByVal ProductId As Integer, ByVal FeatureId As Integer, ByVal SupportValue As Integer) As Boolean
        Try
            Dim connection As New SqlConnection(Me.ConnectionString)
            Dim command As New SqlCommand(DatabaseOwner & ObjectQualifier & "activeapc_dnn_ProductFeatures_Save", connection)
            command.CommandType = CommandType.StoredProcedure
            command.Parameters.Add("@ProductId", SqlDbType.Int).Value = ProductId
            command.Parameters.Add("@FeatureId", SqlDbType.Int).Value = FeatureId
            command.Parameters.Add("@SupportValue", SqlDbType.Int).Value = SupportValue
            connection.Open()
            command.ExecuteNonQuery()
            connection.Close()
            command.Dispose()
            connection.Dispose()
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Public Function SaveProductSortOrder(ByVal ProductId As Integer, ByVal NewPosition As Integer, ByVal OldPosition As Integer) As Boolean
        Try
            Dim connection As New SqlConnection(Me.ConnectionString)
            Dim command As New SqlCommand(DatabaseOwner & ObjectQualifier & "activeapc_dnn_Products_SaveSortOrder", connection)
            command.CommandType = CommandType.StoredProcedure
            command.Parameters.Add("@ProductId", SqlDbType.Int).Value = ProductId
            command.Parameters.Add("@SortOrder", SqlDbType.Int).Value = NewPosition
            command.Parameters.Add("@OldSortOrder", SqlDbType.Int).Value = OldPosition
            connection.Open()
            command.ExecuteNonQuery()
            connection.Close()
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function


#Region "Public Properties"
    Public ReadOnly Property ConnectionString() As String
        Get
            Return Me._connectionString
        End Get
    End Property

    Public ReadOnly Property DatabaseOwner() As String
        Get
            Return Me._databaseOwner
        End Get
    End Property

    Public ReadOnly Property ObjectQualifier() As String
        Get
            Return Me._objectQualifier
        End Get
    End Property
#End Region

#Region "Private Members"
    Private _connectionString As String = Config.GetConnectionString
    Private _databaseOwner As String
    Private _objectQualifier As String
    Private _providerConfiguration As ProviderConfiguration = ProviderConfiguration.GetProviderConfiguration("data")
    Private Const ProviderType As String = "data"
#End Region
End Class

 
